﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Configuration;
using System.IO;
using System.Xml;

using ECCIT;
using OgilvyOne.VW.Beetle.Model;
using OgilvyOne.VW.Beetle.BLL;

namespace OgilvyOne.VW.Beetle.Web
{
    public partial class XmlHttpGate : System.Web.UI.Page
    {
        private const string SUCCESS = "<result>1</result>";
        private const string FAILURE = "<result>0</result>";
        private const string RESULT = "<result>{0}</result>";
        private const string RESULT_MESSAGE = "<result message=\"{0}\">{1}</result>";
        private const string HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "text/xml";
            Response.ContentEncoding = Encoding.UTF8;

            try
            {
                string operation = Request.Params["op"];
                string output = null;
                LikeCountBLL likeBll = new LikeCountBLL();
                
                if (operation == "get_like")
                {
                    string langID = Request.Params["lang"];
                    string sID = Request.Params["id"];
                    if (!string.IsNullOrEmpty(langID) && !string.IsNullOrEmpty(sID))
                    {
                        ResultInfo res = null;
                        int id = int.Parse(sID);
                        if (id > 0)
                        {
                            // 获取单个年代的计数
                            res = likeBll.GetMaxCount(int.Parse(langID), id);
                            if (res.Result)
                                output = HEADER + string.Format(RESULT, (long)res.ReturnObject);
                            else
                                output = HEADER + string.Format(RESULT, -1);
                        }
                        else
                        {
                            // 获取全部年代的计数
                            res = likeBll.GetModels(int.Parse(langID));
                            if (!res.Result)
                            {
                                output = HEADER + string.Format(RESULT_MESSAGE, res.Message, 0);
                            }
                            else
                            {
                                List<LikeCountModel> models = (List<LikeCountModel>)res.ReturnObject;
                                output = "<result>";
                                models.ForEach(delegate(LikeCountModel model)
                                {
                                    output += string.Format("<list years=\"{0}\" lan=\"{1}\">{2}</list>", model.ID, (model.LanguageID == 1033 ? "en" : "cn"), model.Count);
                                });
                                output += "</result>";
                                output = HEADER + output;
                            }
                        }
                    }
                }
                else if (operation == "inc_like")
                {
                    string langID = Request.Params["lang"];
                    string id = Request.Params["id"];
                    if (!string.IsNullOrEmpty(langID) && !string.IsNullOrEmpty(id))
                    {
                        ResultInfo res = likeBll.Increment(int.Parse(langID), int.Parse(id));
                        if (res.Result)
                        {
                            // 获取全部年代的计数
                            res = likeBll.GetModels(int.Parse(langID));
                            if (!res.Result)
                            {
                                output = HEADER + string.Format(RESULT_MESSAGE, res.Message, 0);
                            }
                            else
                            {
                                List<LikeCountModel> models = (List<LikeCountModel>)res.ReturnObject;
                                output = "<result>";
                                models.ForEach(delegate(LikeCountModel model)
                                {
                                    output += string.Format("<list years=\"{0}\" lan=\"{1}\">{2}</list>", model.ID, (model.LanguageID == 1033 ? "en" : "cn"), model.Count);
                                });
                                output += "</result>";
                                output = HEADER + output;
                            }
                        }
                        else
                            output = HEADER + string.Format(RESULT, -1);
                    }
                }

                Response.Write(output);
            }
            catch (ArgumentNullException ane)
            {
                Response.Write("<result error=\"argument is null\">0</result>");
            }
            catch (Exception ex)
            {
                Response.Write("<Error><![CDATA[Error occurred in XmlHttpGate. Phase: 1. Message: " + ex.Message + "]]></Error>");
            }
        }
    }
}